package org.brownflat.fxdate.model;

import org.brownflat.fxdate.util.HashGenerator;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>&copy Rupert Jones 2011</p>
 *
 * @author rup
 */
@Entity
@Table(name = "FXDATE_USER")
public class User extends BaseEntity {
    @Id
    @Column(name="ID")
    @SequenceGenerator(name = "seq", allocationSize = 1, initialValue = 1, sequenceName = "FXDATE_USER_SEQ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq" )
    private long id;

    @Column(name="USERNAME")
    private String username;

    @Column(name = "FULL_NAME")
    private String fullName;

    @Column
    private String password;

    @OneToMany
    @JoinTable(
            name = "FXDATE_USER_ROLE",
            joinColumns = @JoinColumn(name = "USER_ID"),
            inverseJoinColumns = @JoinColumn(name = "ROLE_ID")
    )
    private Set<Role> roles = new HashSet<Role>();


    public void setId(long id) {
        this.id = id;
    }

    public long getId() {
        return this.id;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setCleartextPassword(String password) {
        this.password = HashGenerator.hash(password);
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public void addRole(Role role) {
        this.roles.add(role);
    }
}
